import request from './request';

// 邮件发送服务
export const emailService = {
  // 发送简历上传成功通知
  sendResumeUploadNotification: async (userData, resumeData) => {
    try {
      const emailData = {
        to: userData.email,
        subject: '简历上传成功通知',
        template: 'resume_upload_success',
        data: {
          userName: userData.name,
          resumeName: resumeData.name,
          uploadTime: new Date().toLocaleString(),
          reviewTime: '3个工作日内',
          resumeId: resumeData.id
        }
      };
      
      const response = await request.post('/api/v1/email/send', emailData);
      return response.data;
    } catch (error) {
      console.error('发送简历上传通知邮件失败:', error);
      throw error;
    }
  },
  
  // 发送简历审核结果通知
  sendResumeReviewNotification: async (userData, reviewData) => {
    try {
      const emailData = {
        to: userData.email,
        subject: reviewData.approved ? '简历审核通过通知' : '简历审核结果通知',
        template: reviewData.approved ? 'resume_review_approved' : 'resume_review_rejected',
        data: {
          userName: userData.name,
          resumeName: reviewData.resumeName,
          reviewTime: new Date().toLocaleString(),
          reviewer: reviewData.reviewer,
          score: reviewData.score,
          comments: reviewData.comments,
          nextSteps: reviewData.approved ? '现在可以开始申请实习机会了' : '请根据反馈修改简历后重新提交'
        }
      };
      
      const response = await request.post('/api/v1/email/send', emailData);
      return response.data;
    } catch (error) {
      console.error('发送简历审核通知邮件失败:', error);
      throw error;
    }
  },
  
  // 发送申请推荐通知
  sendApplicationRecommendNotification: async (userData, applicationData) => {
    try {
      const emailData = {
        to: userData.email,
        subject: '申请推荐通知',
        template: 'application_recommend',
        data: {
          userName: userData.name,
          position: applicationData.position,
          company: applicationData.company,
          recommendTime: new Date().toLocaleString(),
          nextSteps: '企业将在3个工作日内查看您的申请，请保持手机畅通'
        }
      };
      
      const response = await request.post('/api/v1/email/send', emailData);
      return response.data;
    } catch (error) {
      console.error('发送申请推荐通知邮件失败:', error);
      throw error;
    }
  },
  
  // 发送企业联系通知
  sendEnterpriseContactNotification: async (userData, contactData) => {
    try {
      const emailData = {
        to: userData.email,
        subject: '企业联系通知',
        template: 'enterprise_contact',
        data: {
          userName: userData.name,
          company: contactData.company,
          position: contactData.position,
          contactTime: new Date().toLocaleString(),
          contactInfo: contactData.contactInfo,
          nextSteps: '请及时查看并主动联系企业HR'
        }
      };
      
      const response = await request.post('/api/v1/email/send', emailData);
      return response.data;
    } catch (error) {
      console.error('发送企业联系通知邮件失败:', error);
      throw error;
    }
  },
  
  // 发送密码修改通知
  sendPasswordChangeNotification: async (userData, changeData) => {
    try {
      const emailData = {
        to: userData.email,
        subject: '密码修改成功通知',
        template: 'password_change',
        data: {
          userName: userData.name,
          changeTime: new Date().toLocaleString(),
          changeIP: changeData.ip,
          deviceInfo: changeData.deviceInfo,
          securityTips: '如果这不是您本人的操作，请立即重新修改密码并联系客服'
        }
      };
      
      const response = await request.post('/api/v1/email/send', emailData);
      return response.data;
    } catch (error) {
      console.error('发送密码修改通知邮件失败:', error);
      throw error;
    }
  },
  
  // 发送申请状态更新通知
  sendApplicationStatusNotification: async (userData, applicationData) => {
    try {
      const statusMessages = {
        submitted: '已提交申请',
        reviewing: '正在审核中',
        interview: '已进入面试阶段',
        offer: '恭喜获得Offer',
        rejected: '很遗憾，申请未通过'
      };
      
      const emailData = {
        to: userData.email,
        subject: `申请状态更新 - ${applicationData.position}`,
        template: 'application_status_update',
        data: {
          userName: userData.name,
          position: applicationData.position,
          company: applicationData.company,
          status: applicationData.status,
          statusMessage: statusMessages[applicationData.status],
          updateTime: new Date().toLocaleString(),
          nextSteps: applicationData.nextSteps || ''
        }
      };
      
      const response = await request.post('/api/v1/email/send', emailData);
      return response.data;
    } catch (error) {
      console.error('发送申请状态更新邮件失败:', error);
      throw error;
    }
  },
  
  // 发送面试邀请通知
  sendInterviewInvitation: async (userData, interviewData) => {
    try {
      const emailData = {
        to: userData.email,
        subject: `面试邀请 - ${interviewData.position}`,
        template: 'interview_invitation',
        data: {
          userName: userData.name,
          position: interviewData.position,
          company: interviewData.company,
          interviewTime: interviewData.interviewTime,
          interviewType: interviewData.type, // online/offline
          interviewLocation: interviewData.location,
          interviewer: interviewData.interviewer,
          preparation: interviewData.preparation || '请提前准备相关材料',
          contactInfo: interviewData.contactInfo
        }
      };
      
      const response = await request.post('/api/v1/email/send', emailData);
      return response.data;
    } catch (error) {
      console.error('发送面试邀请邮件失败:', error);
      throw error;
    }
  },
  
  // 发送Offer通知
  sendOfferNotification: async (userData, offerData) => {
    try {
      const emailData = {
        to: userData.email,
        subject: `恭喜获得Offer - ${offerData.position}`,
        template: 'offer_notification',
        data: {
          userName: userData.name,
          position: offerData.position,
          company: offerData.company,
          salary: offerData.salary,
          startDate: offerData.startDate,
          workLocation: offerData.workLocation,
          benefits: offerData.benefits,
          acceptanceDeadline: offerData.acceptanceDeadline,
          contactPerson: offerData.contactPerson,
          contactEmail: offerData.contactEmail
        }
      };
      
      const response = await request.post('/api/v1/email/send', emailData);
      return response.data;
    } catch (error) {
      console.error('发送Offer通知邮件失败:', error);
      throw error;
    }
  },
  
  // 发送系统通知邮件
  sendSystemNotification: async (userData, systemData) => {
    try {
      const emailData = {
        to: userData.email,
        subject: systemData.subject,
        template: 'system_notification',
        data: {
          userName: userData.name,
          title: systemData.title,
          content: systemData.content,
          sendTime: new Date().toLocaleString(),
          priority: systemData.priority || 'normal',
          actionUrl: systemData.actionUrl || ''
        }
      };
      
      const response = await request.post('/api/v1/email/send', emailData);
      return response.data;
    } catch (error) {
      console.error('发送系统通知邮件失败:', error);
      throw error;
    }
  },
  
  // 发送欢迎邮件
  sendWelcomeEmail: async (userData) => {
    try {
      const emailData = {
        to: userData.email,
        subject: '欢迎加入国际学院实习平台',
        template: 'welcome_email',
        data: {
          userName: userData.name,
          welcomeTime: new Date().toLocaleString(),
          platformFeatures: [
            '简历上传与管理',
            '实习机会浏览',
            '在线申请',
            '面试安排',
            '消息通知'
          ],
          nextSteps: [
            '完善个人资料',
            '上传简历',
            '浏览实习机会',
            '开始申请'
          ]
        }
      };
      
      const response = await request.post('/api/v1/email/send', emailData);
      return response.data;
    } catch (error) {
      console.error('发送欢迎邮件失败:', error);
      throw error;
    }
  },
  
  // 发送密码重置邮件
  sendPasswordResetEmail: async (userData, resetToken) => {
    try {
      const resetUrl = `${window.location.origin}/reset-password?token=${resetToken}`;
      
      const emailData = {
        to: userData.email,
        subject: '密码重置请求',
        template: 'password_reset',
        data: {
          userName: userData.name,
          resetUrl: resetUrl,
          expireTime: '24小时',
          requestTime: new Date().toLocaleString()
        }
      };
      
      const response = await request.post('/api/v1/email/send', emailData);
      return response.data;
    } catch (error) {
      console.error('发送密码重置邮件失败:', error);
      throw error;
    }
  },
  
  // 发送邮箱验证邮件
  sendEmailVerification: async (userData, verificationToken) => {
    try {
      const verificationUrl = `${window.location.origin}/verify-email?token=${verificationToken}`;
      
      const emailData = {
        to: userData.email,
        subject: '邮箱验证',
        template: 'email_verification',
        data: {
          userName: userData.name,
          verificationUrl: verificationUrl,
          expireTime: '24小时',
          sendTime: new Date().toLocaleString()
        }
      };
      
      const response = await request.post('/api/v1/email/send', emailData);
      return response.data;
    } catch (error) {
      console.error('发送邮箱验证邮件失败:', error);
      throw error;
    }
  },
  
  // 批量发送邮件
  sendBulkEmails: async (emailList) => {
    try {
      const response = await request.post('/api/v1/email/bulk-send', {
        emails: emailList
      });
      return response.data;
    } catch (error) {
      console.error('批量发送邮件失败:', error);
      throw error;
    }
  },
  
  // 获取邮件发送状态
  getEmailStatus: async (emailId) => {
    try {
      const response = await request.get(`/api/v1/email/status/${emailId}`);
      return response.data;
    } catch (error) {
      console.error('获取邮件状态失败:', error);
      throw error;
    }
  },
  
  // 获取邮件发送历史
  getEmailHistory: async (params = {}) => {
    try {
      const response = await request.get('/api/v1/email/history', { params });
      return response.data;
    } catch (error) {
      console.error('获取邮件历史失败:', error);
      throw error;
    }
  }
};

// 邮件模板常量
export const EMAIL_TEMPLATES = {
  RESUME_REVIEW_APPROVED: 'resume_review_approved',
  RESUME_REVIEW_REJECTED: 'resume_review_rejected',
  APPLICATION_RECOMMEND: 'application_recommend',
  ENTERPRISE_CONTACT: 'enterprise_contact',
  PASSWORD_CHANGE: 'password_change',
  SYSTEM_NOTIFICATION: 'system_notification',
  WELCOME_EMAIL: 'welcome_email',
  PASSWORD_RESET: 'password_reset',
  EMAIL_VERIFICATION: 'email_verification'
};

// 邮件优先级
export const EMAIL_PRIORITY = {
  LOW: 'low',
  NORMAL: 'normal',
  HIGH: 'high',
  URGENT: 'urgent'
}; 